import json
import os
import sys


import openpyxl

from entity.shipment_json_entity import ShipmentJsonEntity
from ut.read_config import read_computation_sheet_config, get_read_computation_path
from ut.windows_ui import file_path_query, show_warning



def read_shipment_config():
    config=read_computation_sheet_config()
    shipment_json=ShipmentJsonEntity(
        config.get("config",[{}])[2].get("company_name"),
        config.get("config",[{}])[2].get("company_addr"),
        config.get("config",[{}])[2].get("TEL"),
        config.get("config",[{}])[2].get("FAX"),
        config.get("config",[{}])[2].get("customer_name"),
        config.get("config",[{}])[2].get("customer_addr"),
        config.get("config",[{}])[2].get("linkman"),
        config.get("config",[{}])[2].get("contact_number"),
        config.get("config",[{}])[2].get("person_in_charge")
    )
    return shipment_json
def set_shipment_config():
    #此处用表格实现配置信息
    file_path=file_path_query("请选择送货单配置文件")
    wb=openpyxl.load_workbook(file_path,data_only=True)
    ws=wb["Sheet1"]

    #判断空值
    for cells in ws["B3:J3"]:
        for cell in cells:
            if cell.value is None or cell.value == "":
                show_warning("有空值！请填写完整的送货单配置表信息！")
                exit("送货单配置表含空值")



    shipment_data=ShipmentJsonEntity(
        ws["B3"].value,
        ws["C3"].value,
        ws["D3"].value,
        ws["E3"].value,
        ws["F3"].value,
        ws["G3"].value,
        ws["H3"].value,
        ws["I3"].value,
        ws["J3"].value
    )
    config_path=get_read_computation_path()
    with open(config_path, 'r',encoding='utf-8') as f:
        config = json.load(f)
        config["config"][2]["company_name"]=shipment_data.company_name
        config["config"][2]["company_addr"]=shipment_data.company_addr
        config["config"][2]["TEL"]=shipment_data.TEL
        config["config"][2]["FAX"]=shipment_data.FAX
        config["config"][2]["customer_name"]=shipment_data.customer_name
        config["config"][2]["customer_addr"]=shipment_data.customer_addr
        config["config"][2]["linkman"]=shipment_data.linkman
        config["config"][2]["contact_number"]=shipment_data.contact_number
        config["config"][2]["person_in_charge"]=shipment_data.person_in_charge
        try:
            with open(config_path,'w',encoding='utf-8') as f:
                json.dump(config,f,ensure_ascii=False,indent=4)
            show_warning("配置修改成功！")
        except Exception as e:
            show_warning(e)



